import omsRequest from "/utils/omsRequest";
const { showToast } = require("/utils/common");
import { createNewMerchant } from "/api/distributeAchievement";
const { moment } = require("/utils/core");
Component({
  mixins: [],
  data: {
    wholeCountry: {}
  },
  props: {
    list: [],
    vo: {},
    oldvo: {},
    onChangeVo: (vo) => console.log(vo)
  },
  didMount() {
    //
    let res = dd.getStorageSync({ key: 'merchantSubmitTotal' });
    // 回显新签商户数
    // console.log(this.props.list,this.props.vo,"") // 销售总监未分配时候 this.props.list = []
    console.log(this.props.oldvo,"oldvo")
    console.log(res.data.total)
    if (!this.props.oldvo.total || !res.data.total) {
      this.creteNewMerchantMethod()
    }
    this.props.vo.total = res.data.total ? res.data.total : this.props.oldvo.total ? this.props.oldvo.total : 5 //默认值新签商户数5
    this.setData({
      wholeCountry: this.props.vo
    })
  },
  didUpdate() {
  },
  didUnmount() {
    dd.setStorageSync({
      key: 'merchantSubmitTotal',
      data: {
        total: this.data.wholeCountry.total
      }
    });
  },
  methods: {
    onInput(e) {
      this.setData({
        'wholeCountry.total': e.detail.value < 0 ? 0 : e.detail.value
      })
    },
    onSubmit() {
      if (!this.data.wholeCountry.total) {
        showToast({
          msg: '!注意新签商户数还没进行分配'
        })
        return
      }
      this.creteNewMerchantMethod();
    },
    onReset() {
      this.setData({
        'wholeCountry.total': null
      })
    },
    creteNewMerchantMethod() {
      dd.showLoading()
      omsRequest({
        url: createNewMerchant,
        method: "post",
        data: {
          total: this.data.wholeCountry.total ? this.data.wholeCountry.total : 5
        },
        headers: {
          "content-type": "application/json"
        }
      }).then((res) => {
        if (res.success) {
          dd.alert({
            content: '新签商户数分配成功',
            buttonText: '确定',
            success: () => {
              this.setData({
                'wholeCountry.distributableFlag': true // 配置成功
              })
              this.props.onChangeVo(this.data.wholeCountry)
              dd.hideLoading()
            },
          });
        }
      })
    }
  },
});
